package com.hmkj.admin.controller.operate;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.operate.QuestionsModel;
import com.hmkj.core.po.system.Admin;
import com.hmkj.core.service.operate.QuestionsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class QuestionsJsonController extends GenericController {

    @Resource
    private QuestionsService questionsService;

    /**
     * 在线问答json列表
     * @param dtGridPager
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "questions/json/list", method = RequestMethod.POST)
    @RequiresPermissions("sys:questions:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        DtGrid dtgrid = mapper.readValue(dtGridPager, DtGrid.class);
        Map<String, Object> parameters = dtgrid.getFastQueryParameters();
        Admin admin = AdminSessionHelper.getCurrAdmin();
        if(admin.getType()==Admin.TYPE.T3.code){
            parameters.put("eq_agentId",admin.getBussId());
            dtgrid.setFastQueryParameters(parameters);
        }
       dtgrid = questionsService.getDtGridList(dtgrid, QuestionsModel.class);
        if( dtgrid.getExhibitDatas()!=null &&  dtgrid.getExhibitDatas().size()>0){
            List<Object> objects =  new ArrayList<>();
            for (Object obj : dtgrid.getExhibitDatas()) {
                LinkedHashMap map = (LinkedHashMap) obj;
                String str = "";
                Integer replyCost = Integer.valueOf(map.get("replyCost").toString());
                if(replyCost == 0){
                }else if(0<replyCost && replyCost<600000){
                    int second = replyCost/1000;
                    str =   second+"秒";
                }else if(60000<=replyCost && replyCost<3600000){
                    int minute = replyCost/60000;
                    int second = (replyCost - (minute*60000))/1000;
                    str =  minute+"分"+second+"秒";
                }else if (3600000<=replyCost && replyCost<86400000){
                    int hour = replyCost/3600000;
                    int minute = (replyCost - (hour*3600000))/60000;
                    int second = (replyCost - ((hour*3600000)+(minute*60000)))/1000;
                    str =  hour+"小时"+minute+"分"+second+"秒";
                }else{
                    int day = replyCost/86400000;
                    str =  day+"天";
                }
                map.put("replyCostStr",str);
                objects.add(map);
            }
            dtgrid.setExhibitDatas(objects);
         }
        return dtgrid;

    }

    /**
     * 回复问题
     */
    @ResponseBody
    @RequestMapping(value = "questions/json/reply", method = RequestMethod.POST)
    @RequiresPermissions("sys:questions:reply")
    public ResultEntity reply(Integer id,String answer) throws Exception {
        if (null ==id){
            throw new BussinessException("参数有误");
        }
        if(!StringUtils.isNotBlank(answer)){
            throw new BussinessException("回复不能为空");
        }
        Admin admin = AdminSessionHelper.getCurrAdmin();
        questionsService.replayQuestion(id,answer,admin.getId());
       return ok();
    }



}
